package com.hornsun.medicine.API;

import com.google.common.base.Preconditions;
import com.hornsun.domain.dbo.Medicine;
import com.hornsun.repository.MedicineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/1/29.
 */
@RestController
@RequestMapping("/api/medicine")
public class MedicineAPI {
    @Autowired
    private MedicineRepository medicineRepository;
    /**
     * 增加药品信息
     * @param medicine
     */
    @PostMapping("/addMedicine")
    public ResponseEntity<?> addMedicine(@RequestParam Medicine medicine){
        medicine.setMedicineGuid(UUID.randomUUID().toString());
        medicineRepository.insertMedicine(medicine);
        return new ResponseEntity<>(HttpStatus.OK);

    }

    /**
     * 修改药品信息
     * @param medicine
     */
    @PostMapping("/updateMedicine")
    public ResponseEntity<?> updateMedicine(@RequestParam Medicine medicine){
        medicineRepository.updateMedicine(medicine);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除药品信息
     */
    @PostMapping("/deleteMedicine")
    public ResponseEntity<?> deleteMedicine(@RequestParam("guid") String guid){
        medicineRepository.deleteMedicine(guid);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 查询药品信息
     * @param name
     * @return
     */
    @GetMapping("/queryMedicine")
    public ResponseEntity<List<Medicine>> queryMedicine(@RequestParam String name){
        List<Medicine> list = medicineRepository.getMedicine(name);
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

}
